package cn.ytr.crm.service.impl;

import cn.ytr.crm.domain.SystemDictionaryItem;
import cn.ytr.crm.mapper.SystemDictionaryItemMapper;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService{

	@Autowired
	private SystemDictionaryItemMapper systemDictionaryItemMapper;

	/**
	 * 增加或保存字典明细
	 */
	@Override
	public void saveOrUpate(SystemDictionaryItem systemDictionaryItem) {
		//如果排序字段为空，则自动给它所属目录里的明细排序最大的再往后的一位
        if (systemDictionaryItem.getSequence() == null) {
            int maxSequence = systemDictionaryItemMapper.selectMaxSequence(systemDictionaryItem.getParentId());
            systemDictionaryItem.setSequence(maxSequence + 1);
        }

		if (systemDictionaryItem.getId() != null) {
			systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
		} else {
			systemDictionaryItemMapper.insert(systemDictionaryItem);
		}
	}

	/**
	 * 删除字典明细
	 */
	@Override
	public void delete(Long id) {
		systemDictionaryItemMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取单个字典明细
	 */
	@Override
	@Transactional(readOnly=true)
	public SystemDictionaryItem get(Long id) {
		SystemDictionaryItem systemDictionaryItem = systemDictionaryItemMapper.selectByPrimaryKey(id);
		return systemDictionaryItem;
	}

	/**
	 * 获取所有字典明细
	 */
	@Override
	@Transactional(readOnly=true)
	public List<SystemDictionaryItem> listAll() {
		List<SystemDictionaryItem> list = systemDictionaryItemMapper.selectAll();
		return list;
	}

	/**
	 * 高级查询所有字典明细
	 */
	@Override
	@Transactional(readOnly=true)
	public PageInfo query(QueryObject qo) {
		/*Integer count = systemDictionaryItemMapper.selectForCount(qo);
		if (count < 1 || count == null) {
			return new PageResult(qo.getPageSize());
		} else {
			List<SystemDictionaryItem> list = systemDictionaryItemMapper.selectForList(qo);
			return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
		}*/

		//改用分页插件 pageHelper
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<SystemDictionaryItem> list = systemDictionaryItemMapper.selectForList(qo);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}

	/**
	 * 通过字典目录查询字典明细
	 */
	public List<SystemDictionaryItem> selectByDicSn(String job) {
		return systemDictionaryItemMapper.selectByDicSn(job);
	}

}













































